package com.cafuc.myapplicationcat;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

public class CommonDialog extends AlertDialog {
    private TextView titleTv,messageTv;
    private Button negtiveBn, positiveBn;
    protected CommonDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        initView();
        /**
         * 初始化控件点击事件
         */
        intEvent();
    }

    public interface onClickBottomListener{
        void onPositiveClick();//实现按钮点击事件的方法
        void onNegtiveClick();
    }

    public onClickBottomListener onClickBottomListener;

    public CommonDialog setOnClickBottomListener(onClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    private void intEvent() {
        positiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickBottomListener!=null){
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        negtiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickBottomListener!=null){
                    onClickBottomListener.onNegtiveClick();

                }
            }
        });
    }

    private void initView() {
        titleTv = (TextView) findViewById(R.id.title_Cus);
        messageTv = (TextView) findViewById(R.id.message_Cus);
        negtiveBn = (Button) findViewById(R.id.negtive);
        positiveBn = (Button) findViewById(R.id.positive);
    }
private String message,title,positive, negtive;
    public  CommonDialog setMessage(String message){
        this.message = message;
        return this;
    }
    public CommonDialog setTitle(String title){
        this.title = title;
        return this;
    }

    public CommonDialog setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    public CommonDialog setNegtive(String negtive) {
        this.negtive = negtive;
        return this;
    }

    /**
     * 初始化控件的显示数据
     */
    public void refreshView(){
        //如果设置了title就会将设置的数据传入控件，并且显示；没有设置则会隐藏
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);//显示
        }else {
            titleTv.setText(View.GONE);//隐藏
        }
        if (!TextUtils.isEmpty(message)){
            messageTv.setText(message);
        }
        if (!TextUtils.isEmpty(positive)) {//设置按钮的文本为自定义显示
            positiveBn.setText(positive);

        } else {
            positiveBn.setText("ok");
        }
        if (!TextUtils.isEmpty(negtive)) {

            negtiveBn.setText(negtive);
        } else {
            negtiveBn.setText("cancel");
        }
    }

    public void show() {
        super.show();
        refreshView();
    }

}
